﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.DB;
using ExcelLibrary.SpreadSheet;
using RMS.Common;
using ExcelLibrary.CompoundDocumentFormat;
using System.IO;
using System.Diagnostics;

namespace RMS
{
    public partial class frmBackup : ICommonForm
    {
        public frmBackup()
        {
            InitializeComponent();
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            InventoryMng mng = new InventoryMng();
            var list = mng.GetAllInventory();

            if (list != null && list.Count() > 0)
            {
                Workbook workbook = new Workbook();
                Worksheet worksheet = new Worksheet("Product List");

                //Header Row
                worksheet.Cells[0, 0] = new Cell("Mã sản phẩm");
                worksheet.Cells[0, 1] = new Cell("Tên sản phẩm");
                worksheet.Cells[0, 2] = new Cell("Số lượng");
                worksheet.Cells[0, 3] = new Cell("Giá nhập");
                worksheet.Cells[0, 4] = new Cell("Giá bán");
                worksheet.Cells[0, 5] = new Cell("Tổng giá nhập");
                worksheet.Cells[0, 6] = new Cell("Nhà cung cấp");
                worksheet.Cells[0, 7] = new Cell("Nhóm");
                worksheet.Cells[0, 8] = new Cell("Kiểu");
                worksheet.Cells[0, 9] = new Cell("Trọng lượng (gam)");
                worksheet.Cells[0, 10] = new Cell("Thể tích (ml)");
                worksheet.Cells[0, 11] = new Cell("Tên file ảnh");
                worksheet.Cells[0, 12] = new Cell("Mã đơn hàng");
                worksheet.Cells[0, 13] = new Cell("Mã lô");
                worksheet.Cells[0, 14] = new Cell("Xuất sứ");
                worksheet.Cells[0, 15] = new Cell("Seri");
                worksheet.Cells[0, 16] = new Cell("Miêu tả");
                

                int startRow = 1;
                foreach (Inventory item in list)
                {
                    worksheet.Cells[startRow, 0] = new Cell(item.ID);
                    worksheet.Cells[startRow, 1] = new Cell(item.Name);
                    worksheet.Cells[startRow, 2] = new Cell(item.Quantity);
                    worksheet.Cells[startRow, 3] = new Cell(item.ImportPrice);
                    worksheet.Cells[startRow, 4] = new Cell(item.SalePrice);
                    worksheet.Cells[startRow, 5] = new Cell(item.TotalPrice);
                    worksheet.Cells[startRow, 6] = new Cell(item.SupplierID);
                    worksheet.Cells[startRow, 7] = new Cell(item.Category);
                    worksheet.Cells[startRow, 8] = new Cell(item.StyleId);
                    worksheet.Cells[startRow, 9] = new Cell(item.Weight);
                    worksheet.Cells[startRow, 10] = new Cell(item.Volume);
                    worksheet.Cells[startRow, 11] = new Cell(item.Image);
                    worksheet.Cells[startRow, 12] = new Cell(item.InvoiceNo);
                    worksheet.Cells[startRow, 13] = new Cell(item.MemoNo);
                    worksheet.Cells[startRow, 14] = new Cell(item.ItemNo);
                    worksheet.Cells[startRow, 15] = new Cell(item.StyleNo);
                    worksheet.Cells[startRow, 16] = new Cell(item.Description);

                    startRow += 1;
                    
                }

                workbook.Worksheets.Add(worksheet);

                string uniqueString = Utilities.UniqueString();
                string fileBackup = String.Format("{0}{1}Backup_{2}.xls", Environment.CurrentDirectory, Constants.BackupFolderPath, uniqueString);
                workbook.Save(fileBackup);
                //open created file
                //CompoundDocument.Open(folderBackup);
                Process.Start(fileBackup);

                //Copy data file
                string databaseFilePath = String.Format("{0}\\DB\\data.db", Environment.CurrentDirectory);
                string databaseFileToBackupPath = String.Format("{0}{1}data_{2}.db", Environment.CurrentDirectory, Constants.BackupFolderPath, uniqueString);
                if(File.Exists(databaseFilePath))
                {
                    File.Copy(databaseFilePath, databaseFileToBackupPath);
                }

                btnProcess.Enabled = false;
                
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
